<?php
/**
* @version		$Id: view.html.php 8117 2007-07-20 13:37:22Z friesengeist $
* @package		Joomla
* @subpackage	Weblinks
* @copyright	Copyright (C) 2005 - 2007 Open Source Matters. All rights reserved.
* @license		GNU/GPL, see LICENSE.php
* Joomla! is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*/

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();

jimport( 'joomla.application.component.view');
jimport( 'joomla.utilities.date' );

/**
 * HTML View class for the WebLinks component
 *
 * @static
 * @package		Joomla
 * @subpackage	Weblinks
 * @since 1.0
 */
class BookstoreViewStaff extends JView
{
	function display($tpl = null)
	{
		global $mainframe, $option;
		//var_dump('view1');
		if($this->getLayout() == 'form') {
			//var_dump('switch');
			$this->_displayForm($tpl);
			return;
		}
		
		//var_dump('display');
		// Set toolbar items for the page
		JToolBarHelper::title(   JText::_( 'Bookstore Staff Manager' ), 'generic.png' );
		JToolBarHelper::publishList();
		JToolBarHelper::unpublishList();
		JToolBarHelper::deleteList();
		JToolBarHelper::editListX();
		JToolBarHelper::addNewX();
		//JToolBarHelper::preferences('com_bookstore', '360');
		JToolBarHelper::help( 'screen.bookstore' );

		$db		=& JFactory::getDBO();
		$uri	=& JFactory::getURI();

		$filter_state		= $mainframe->getUserStateFromRequest( $option.'filter_state',		'filter_state',		'',				'word' );
		$filter_catid		= $mainframe->getUserStateFromRequest( $option.'filter_catid',		'filter_catid',		0,				'int' );
		$filter_order		= $mainframe->getUserStateFromRequest( $option.'filter_order',		'filter_order',		'a.ordering',	'cmd' );
		$filter_order_Dir	= $mainframe->getUserStateFromRequest( $option.'filter_order_Dir',	'filter_order_Dir',	'',				'word' );
		$search				= $mainframe->getUserStateFromRequest( $option.'search',			'search',			'',				'string' );
		$search				= JString::strtolower( $search );
		
		//var_dump($search);
		$model	=& $this->getModel('staff');
		// Get data from the model
		//var_dump('viewa');
		
		$items		= & $model->getData();
		$total		= & $model->getTotal();
		$pagination = & $model->getPagination();
		/*
		$items		= & $this->get( 'Data');
		$total		= & $this->get( 'Total');
		$pagination = & $this->get( 'Pagination' );
		*/
		//var_dump('view2');
		// build list of categories
		$javascript 	= 'onchange="document.adminForm.submit();"';
		$lists['catid'] = JHTML::_('list.category',  'filter_catid', $option, intval( $filter_catid ), $javascript );

		// state filter
		$lists['state']	= JHTML::_('grid.state',  $filter_state );

		// table ordering
		$lists['order_Dir'] = $filter_order_Dir;
		$lists['order'] = $filter_order;

		// search filter
		$lists['search']= $search;

		$this->assignRef('user',		JFactory::getUser());
		$this->assignRef('lists',		$lists);
		$this->assignRef('items',		$items);
		$this->assignRef('pagination',	$pagination);
		$this->assignRef('request_url',	$uri->toString());

		parent::display($tpl);
	}
	
	
	function _displayForm($tpl)
	{
		global $mainframe, $option;
		//var_dump('displayform');
		//jimport('joomla.html.pane');
		
		$db		=& JFactory::getDBO();
		$uri 	=& JFactory::getURI();
		$user 	=& JFactory::getUser();
		$model	=& $this->getModel('staff');
		//$pane	=& JPane::getInstance('sliders');
		
		//JHTML::_('behavior.tooltip');
		//JHTML::_('behavior.calendar');		
		
		$lists = array();

		//get the weblink
		//var_dump($model);
		//$model->_build('detail');
		//$model->_setDetail;
		//var_dump('displayform 1');
		
		//JToolBarHelper::title(   JText::_( 'Calendar Manager' ), 'generic.png' );
        //JToolBarHelper::deleteList();
        //JToolBarHelper::editListX();
        //JToolBarHelper::addNewX();
		//$data =& $this->get('Data');
		//$data = $model->get('Data');
		$isNew		= ((int)$model->_id < 1);
		//var_dump($isNew);
		if($isNew){
			$now = new JDate();
		//	$selection = new stdclass();
			//var_dump($now);
			$selection->created = $now->toMySQL();
		}else{
			$data = $model->getData('detail');
			//var_dump($model);
			$selection = (is_array($data)) ? $data[0] : $data;
		}
		/**/
		/*
		 * We need to unify the introtext and fulltext fields and have the
		 * fields separated by the {readmore} tag, so lets do that now.
		 */
		
		if (JString::strlen($selection->fulldescription) > 1) {
			$selection->description = $selection->introdescription . "<hr id=\"system-readmore\" />" . $selection->fulldescription;
		} else {
			$selection->description = $selection->introdescription;
		}
		 /**/
		/**
		* patch to bridge use of param date and startdate column
		*/
		
		$selection->reg = new JRegistry('com_bookstore');
		$selection->reg->loadini($selection->params);
		
		//var_dump('displayform 2');
		
		//var_dump($selection->id);
		// fail if checked out not by 'me'
		/**/
		/*
		if ($model->isCheckedOut( $user->get('id') )) {
			$msg = JText::sprintf( 'DESCBEINGEDITTED', JText::_( 'The weblink' ), $selection->title );
			$mainframe->redirect( 'index.php?option='. $option, $msg );
		}
		*/
		
		// Set toolbar items for the page
		
		$text = $isNew ? JText::_( 'New' ) : JText::_( 'Edit' );
		JToolBarHelper::title(   JText::_( 'Staff Selection' ).': <small><small>[ ' . $text.' ]</small></small>' );
		JToolBarHelper::save();
		if ($isNew)  {
			JToolBarHelper::cancel();
		} else {
			// for existing items the button is renamed `close`
			JToolBarHelper::cancel( 'cancel', 'Close' );
		}
		JToolBarHelper::help( 'screen.bookstore.edit' );

		// Edit or Create?
		//var_dump($isNew);
		
		if (!$isNew)
		{
			//retreive isbn indices
			$indices =& $this->get('Indices');
			//$model->checkout( $user->get('id') );
			//$var = $model->deleteIndices();
			//echo $var;
		}
		else
		{
			// initialise new record
			$selection->published = 1;
			$selection->approved 	= 1;
			$selection->order 	= 0;
			$selection->catid 	= JRequest::getVar( 'catid', 0, 'post', 'int' );
		}

		// build the html select list for ordering
		//var_dump('displayform 3');
		$query = 'SELECT ordering AS value, title AS text'
			. ' FROM #__staffselection'
			//. ' WHERE catid = ' . (int) $calendar->catid
			. ' ORDER BY ordering';
		//var_dump('displayform 3.1');
		$lists['ordering'] 			= JHTML::_('list.specificordering',  $selection, $selection->id, $query, 1 );
		if(substr($lists['ordering'], 0, 57) == '<input type="hidden" name="ordering" value="" />New items')
		{
		$lists['ordering'] 			= "Items are ordered by date, but giving a default ordering number of '0'. Ordering can be changed after this item is saved.";
		}
		//var_dump('displayform 3.2');
		// build list of categories
		$lists['catid'] 			= JHTML::_('list.category',  'catid', $option, intval( $selection->catid ) );
		// build the html select list
		$lists['published'] 		= JHTML::_('select.booleanlist',  'published', 'class="inputbox"', $selection->published );
		
		///////////
		$file 	= JPATH_ADMINISTRATOR .'/components/com_bookstore/models/selection.xml';
		$params = new JParameter( $selection->params, $file, 'component' );
		/**/
		$this->assignRef('lists',		$lists);
		$this->assignRef('selection',		$selection);
		$this->assignRef('params',		$params);
		$this->assignRef('time',		$time_selector_obj);
		$this->assignRef('request_url',	$uri->toString());

		
		parent::display($tpl);
	}
}
?>
